//flight class implementation
#include "flight.h"
#include <sstream>
using std::stringstream;

//default constructor
flight::flight() :
flightNumber(-1),
departureTime(-1),
arrivalTime(-1),
departureCity(""),
arrivalCity(""),
isCancelled(true)
{};

//constructor
flight::flight(int fNumber, time_t dTime, time_t aTime, string dCity, string aCity, bool cancel, vector<vector<bool> > seating) :
flightNumber(fNumber),
departureTime(dTime),
arrivalTime(aTime),
departureCity(dCity),
arrivalCity(aCity),
seatingPlan(seating),
isCancelled(cancel)
{};

//destructor
flight::~flight(){};

int flight::getFlightNumber() const
{
	return flightNumber;
};

time_t flight::getDepartureTime() const
{
	return departureTime;
};

void flight::setDepartureTime(time_t t)
{
	departureTime = t;
};

time_t flight::getArrivalTime() const
{
	return arrivalTime;
};

void flight::setArrivalTime(time_t t)
{
	arrivalTime = t;
};

string flight::getDepartureCity() const
{
	return departureCity;
};

string flight::getArrivalCity() const
{
	return arrivalCity;
};

bool flight::seatsAvailable()
{
	for(vector<vector<bool> >::iterator i = seatingPlan.begin(); i != seatingPlan.end(); ++i){
		vector<bool> row = *i;
		for(vector<bool>::iterator j = row.begin(); j != row.end(); ++j){
			bool seatTaken = *j;
			if(!seatTaken) return true;
		}
	}

	return false;
};

string flight::getSeat()
{
	if(!seatsAvailable()) return ""; //All seats taken, nothing to return

	for(int i = 0; i < int(seatingPlan.size()); ++i){
		vector<bool> row = seatingPlan[i];
		for(int j = 0; j < int(row.size()); ++j){
			bool seatTaken = seatingPlan[i][j];
			if(!seatTaken){
				seatingPlan[i][j] = true; //seat is now taken
				
				string seat;
				switch(j){
					case 0: seat = "A"; break;
					case 1: seat = "B"; break;
					case 2: seat = "C"; break;
					case 3: seat = "D"; break;
					default: seat = "X"; break;
				}
				stringstream ss;
				ss << (i + 1);
				seat += ss.str();
				return seat;
			}
		}
	}

	return ""; //All seats taken, nothing to return
};

//return 0 is seat freed successfully
//return -1 on fail
int flight::freeSeat(string seat)
{
	if(seat.length() != 2) return -1; //not a valid seat number;
	
	char c = seat[0];
	int col;
	switch(c){
		case 'A': col = 0; break;
		case 'B': col = 1; break;
		case 'C': col = 2; break;
		case 'D': col = 3; break;
			break;
		default: return -1; //not a valid seat number;
	}

	string r = seat.substr(1);
	int row = atoi(r.c_str());

	if(row < 1 || row > 20) return -1; //not a valid seat number;
	--row;

	bool seatTaken = seatingPlan[row][col];
	if(seatTaken){
		seatingPlan[row][col] = false; //free seat
		return 0; //seat freed successfully
	}
	else return -1; //seat already free
};

vector<vector<bool> > flight::getSeatingPlan() const
{
	return seatingPlan;
};

bool flight::is_Cancelled() const
{
	return isCancelled;
};

void flight::cancel()
{
	isCancelled = true;
};

void flight::setDepartureCity(string dCity){
	departureCity = dCity;
};

void flight::setArrivalCity(string aCity)
{
	arrivalCity = aCity;
};
